----------------------------------------------------------------------------------------------------------------------------------
--[[

    

]]--
----------------------------------------------------------------------------------------------------------------------------------
---
    local function GetReplica(self)
        return self.inst.replica.loramia_com_tent or self.inst.replica._.loramia_com_tent
    end
    local function set_custom_key_board_event(self,flag)
        local replica_om = GetReplica(self)
        if replica_om then
            replica_om:SetCustomKeyboardEvent(flag)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_tent = Class(function(self, inst)
    self.inst = inst

    self.sleepers = {}

    ----------------------------------------------------
    --- 玩家客户端上传 事件
        self.inst:ListenForEvent("player_command_wake_up",function(inst,_table)
            local userid = _table.userid
            print("server player_command_wake_up",userid)
            if userid then
                for temp_player, flag in pairs(self.sleepers) do
                    if temp_player and temp_player.userid == userid and flag then
                        self:PlayerLeave(temp_player)
                        return
                    end
                end
            end
        end)
    ----------------------------------------------------
    ---
        self.inst:ListenForEvent("onremove",function()
            for temp_player, flag in pairs(self.sleepers) do
                if temp_player and flag then
                    self:PlayerLeave(temp_player)
                end
            end
        end)
    ----------------------------------------------------
    ---
        self.custom_key_board_event = false
    ----------------------------------------------------
end,
nil,
{
    custom_key_board_event = set_custom_key_board_event,
})
------------------------------------------------------------------------------------------------------------------------------
---
    function loramia_com_tent:SetEnterTestFn(fn)
        self.enter_test_fn = fn
    end
    function loramia_com_tent:SetOnEnterFn(fn)
        self.on_enter_fn = fn
    end
    function loramia_com_tent:SetOnLeaveFn(fn)
        self.on_leave_fn = fn
    end
------------------------------------------------------------------------------------------------------------------------------
--- 下发事件，激活键盘监听
    function loramia_com_tent:SetCustomKeyboardEvent(flag)
        self.custom_key_board_event = flag
    end
    function loramia_com_tent:AcitveKeyBoardEventListener(player)
        local rpc = player.components.loramia_com_rpc_event
        if rpc then
            rpc:PushEvent("acive_key_board_event_listener",{userid = player.userid},self.inst)
        end
    end
    function loramia_com_tent:DeacitveKeyBoardEventListener(player)
        local rpc = player.components.loramia_com_rpc_event
        if rpc then
            rpc:PushEvent("deactive_key_board_event_listener",{userid = player.userid},self.inst)
        end
    end    
------------------------------------------------------------------------------------------------------------------------------
--- 获取玩家
    function loramia_com_tent:GetAllPlayers()
        local ret = {}
        for temp_player, flag in pairs(self.sleepers) do
            if temp_player and flag and temp_player:IsValid() then
                table.insert(ret,temp_player)
            end
        end
        return ret,#ret
    end
    function loramia_com_tent:HasPlayerPrefab(prefab)
        local ret = self:GetAllPlayers()
        for k, v in pairs(ret) do
            if v and v.prefab == prefab then
                return true
            end
        end
    end
------------------------------------------------------------------------------------------------------------------------------
--- 复制来的api
    local function SetSleeperSleepState(inst)
        if inst.components.grue ~= nil then
            inst.components.grue:AddImmunity("sleeping")
        end
        if inst.components.talker ~= nil then
            inst.components.talker:IgnoreAll("sleeping")
        end
        if inst.components.firebug ~= nil then
            inst.components.firebug:Disable()
        end
        if inst.components.playercontroller ~= nil then
            inst.components.playercontroller:EnableMapControls(false)
            inst.components.playercontroller:Enable(false)
        end
        inst:OnSleepIn()
        inst.components.inventory:Hide()
        inst:PushEvent("ms_closepopups")
        inst:ShowActions(false)
        -- inst:AddTag("ignoretalking")
        inst.sg:Stop()
    end

    local function SetSleeperAwakeState(inst)
        if inst.components.grue ~= nil then
            inst.components.grue:RemoveImmunity("sleeping")
        end
        if inst.components.talker ~= nil then
            inst.components.talker:StopIgnoringAll("sleeping")
        end
        if inst.components.firebug ~= nil then
            inst.components.firebug:Enable()
        end
        if inst.components.playercontroller ~= nil then
            inst.components.playercontroller:EnableMapControls(true)
            inst.components.playercontroller:Enable(true)
        end
        inst:OnWakeUp()
        inst.components.inventory:Show()
        inst:ShowActions(true)
        -- inst:RemoveTag("ignoretalking")
        inst.sg:Start()
    end
------------------------------------------------------------------------------------------------------------------------------
--- PlayerEnter
    function loramia_com_tent:PlayerEnter(player)
        if self.enter_test_fn == nil or not self.enter_test_fn(self.inst, player) then
            return false
        end
        ---------------------------------------------------------------
        --
            self.sleepers[player] = true
        ---------------------------------------------------------------
        --
            SetSleeperSleepState(player)
        ---------------------------------------------------------------
        --
            player:Hide()            
            if player.components.health then
                player.components.health:SetInvincible(true)
            end
            if player.Physics ~= nil then
                player.Physics:Teleport(self.inst.Transform:GetWorldPosition())
            end
            if player.DynamicShadow ~= nil then
                player.DynamicShadow:Enable(false)
            end
            player.sg:AddStateTag("sleeping")
            player.sg:AddStateTag("silentmorph")
            player.sg:RemoveStateTag("busy")
        ---------------------------------------------------------------
        ---
            self:AcitveKeyBoardEventListener(player)
        ---------------------------------------------------------------
        ---
            if self.on_enter_fn then
                self.on_enter_fn(self.inst, player)
            end
        ---------------------------------------------------------------
        ---
            self.inst:PushEvent("loramia_com_tent.PlayerEnter",player)
        ---------------------------------------------------------------
        return true
    end
------------------------------------------------------------------------------------------------------------------------------
--- PlayerLeave
    function loramia_com_tent:PlayerLeave(player)
        ---------------------------------------------------------------
        ---
        ---------------------------------------------------------------
        ---
            if not self.sleepers[player] then
                return
            end
        ---------------------------------------------------------------
        ---
            self.sleepers[player] = nil
            self:DeacitveKeyBoardEventListener(player)
        ---------------------------------------------------------------
        ---
            player:Show()
            if player.components.health then
                player.components.health:SetInvincible(false)
            end
            if player.DynamicShadow ~= nil then
                player.DynamicShadow:Enable(true)
            end
        ---------------------------------------------------------------
        ---
            SetSleeperAwakeState(player)
        ---------------------------------------------------------------
        ---
            if player.Physics ~= nil then
                player.Physics:Teleport(self.inst.Transform:GetWorldPosition())
            end
            player.sg:GoToState("wakeup")
        ---------------------------------------------------------------
        ---
            if self.on_leave_fn then
                self.on_leave_fn(self.inst, player)
            end
        ---------------------------------------------------------------
        ---
            self.inst:PushEvent("loramia_com_tent.PlayerLeave",player)            
        ---------------------------------------------------------------
    end
------------------------------------------------------------------------------------------------------------------------------
--- All player leave
    function loramia_com_tent:AllPlayerLeave()
        local all_players = self:GetAllPlayers()
        for k, v in pairs(all_players) do
            self:PlayerLeave(v)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_tent







